<?php

namespace App\Http\Controllers;

use App\Models\Org;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;


class OrgSettingController extends Controller
{
    public function __construct(Request $request)
    {
        $user = $request->user();

        if (!$user || $user->type != 4) {
            throw new \Exception('权限不足');
        }

        parent::__construct();
    }


    public function saveConfigSettings(Request $request, $id) {
        $this->validate($request, [
            'key' => 'required|max:32',
            'type' => 'required|max:32',
            'label' => 'required|max:32',
            'is_required' => 'required:64'
        ]);
        $user = $request->user();

        if (!($user->type === 4)) {
            throw new \Exception('权限不足');
        }

        $node = Org::findOrFail($id);
        $data = $request->only([
            'key',
            'label',
            'type',
            'options',
            'is_required',
        ]);

        $res = $node->addCustomConfigExtends($data);

//        dd($node);
        if ($res) {
            $node->save();
            return $this->returnJSON(null, 0, "添加成功");
        } else {
            return new JsonResponse([
                'status' => 422,
                'msg' => '验证不通过',
                'errors' => ['key' => "所填写标识已经存在。"]
            ], 200);
        }
    }

    public function editConfigSettings(Request $request, $id) {
        $this->validate($request, [
            'key' => 'required|max:32',
            'type' => 'required|max:32',
            'label' => 'required|max:32',
            'is_required' => 'required:64'
        ]);
        $user = $request->user();

        if (!($user->type === 4)) {
            throw new \Exception('权限不足');
        }

        $node = Org::findOrFail($id);
        $data = $request->only([
            'key',
            'label',
            'type',
            'options',
            'is_required',
        ]);

        $res = $node->editCustomConfigExtends($data);

        if ($res) {
            $node->save();
            return $this->returnJSON(null, 0, "修改成功");
        } else {
            return $this->returnJSON(null, 422, "修改失败");
        }
    }

    public function getConfigSettings(Request $request, $id) {
        $user = $request->user();

        if (!($user->type === 4)) {
            throw new \Exception('权限不足');
        }

        $node = Org::findOrFail($id);
        $data = $node->getCustomConfigExtends();
        return $this->returnJSON($data, 0, '');
    }

    public function deleteConfigSettings(Request $request, $id) {
        $this->validate($request, [
            'key' => 'required|max:32'
        ]);
        $user = $request->user();

        if (!($user->type === 4)) {
            throw new \Exception('权限不足');
        }

        $node = Org::findOrFail($id);
        $res = $node->deleteCustomConfigExtends($request->get('key'));
        if ($res) {
            $node->save();
            return $this->returnJSON(null, 0, "删除成功");
        } else {
            return $this->returnJSON(null, 422, "删除失败");
        }
    }

    public function sortConfigSettings(Request $request, $id) {
        $this->validate($request, [
            'rows' => 'required|array',
        ]);
        $user = $request->user();

        if (!($user->type === 4)) {
            throw new \Exception('权限不足');
        }

        $node = Org::findOrFail($id);

        $res = $node->sortCustomConfigExtends($request->get('rows'));

        if ($res) {
            $node->save();
            return $this->returnJSON(null, 0, "修改成功");
        } else {
            return $this->returnJSON(null, 422, "修改失败");
        }
    }

    public function getOpenConfigSettings(Request $request, $id) {

        $user = $request->user();

        if (!($user->type === 4)) {
            throw new \Exception('权限不足');
        }

        $node = Org::findOrFail($id);
        $data = $node->getOpenCustomConfigExtends();
        $data['real_name_register'] = $node->real_name_register ? 1 : 0;
//        dd($data,gettype($data));

        return $this->returnJSON($data, 0, '');
    }

    public function setOpenConfigSettings(Request $request, $id) {
        $this->validate($request, [
            'real_name_register' => 'int:0,1',
            'open_real_name' => 'int:0,1',
            'open_avatars' => 'int:0,1',
            'open_id_number' => 'int:0,1',
            'open_nation' => 'int:0,1',
            'open_addr' => 'int:0,1'
        ]);
        $user = $request->user();

        if (!($user->type === 4)) {
            throw new \Exception('权限不足');
        }

        $node = Org::findOrFail($id);
        $node->fill(['real_name_register'=>$request->input('real_name_register')]);
        $data = $request->only([
            'open_real_name',
            'open_avatars',
            'open_id_number',
            'open_nation',
            'open_addr'
        ]);
        $node->setOpenCustomConfigExtends($data);
        $node->save();

        return $this->returnJSON('保存成功', 0, '');
    }
}
